home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
LANG
/
C
/
LIB
/
UNIXLIB37B
/
!UnixLib37
/
src
/
unix
/
c
/
getenv
< prev
next >
Wrap
Text File
|
1996-11-09
|
4KB
|
238 lines
/****************************************************************************
*
* $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/getenv,v $
* $Date: 1996/10/30 21:59:01 $
* $Revision: 1.3 $
* $State: Rel $
* $Author: unixlib $
*
* $Log: getenv,v $
* Revision 1.3 1996/10/30 21:59:01 unixlib
* Massive changes made by Nick Burret and Peter Burwood.
*
* Revision 1.2 1996/05/06 09:01:35 unixlib
* Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
* Saved for 3.7a release.
*
* Revision 1.1 1996/04/19 21:35:27 simon
* Initial revision
*
***************************************************************************/
static const char rcs_id[] = "$Id: getenv,v 1.3 1996/10/30 21:59:01 unixlib Rel $";
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/os.h>
#include <sys/unix.h>
#include <sys/swis.h>
/* These global variables are also initialised in unix.c.
These initialisations are for safety purposes incase one of the functions
in this file is called before unixinit. */
char **environ = NULL;
int __envcnt = 0, __envsiz = 1;
static int
__cmpenv (register char *s1, register const char *s2)
{
register int i, j;
while ((i = *s1) && i != '=' && i == *s2)
s1++, s2++;
if (i == '=')
i = 0;
if ((j = *s2) == '=')
j = 0;
return (i - j);
}
char *
__addenv (const char *s, char *v)
{
register char **e, *t;
register int i, j, k;
if (!environ)
{
/* This shouldn't be possible, unless we've been called
prior to unixinit setting environ. */
if (!(environ = malloc (sizeof (char *))))
return NULL;
*environ = NULL;
__envcnt = 0;
__envsiz = 1;
}
i = strlen (s) + 1;
if (v)
{
j = strlen (v) + 1;
k = i + j;
}
else
{
k = i;
t = (char *) s;
while (*++t != '=');
i = t - s + 1;
j = k - i;
}
for (e = environ; t = *e; e++)
if (!__cmpenv (t, s))
{
if (!(*e = t = realloc (t, k)))
return NULL;
goto add;
}
if (!(t = environ[__envcnt++] = malloc (k)))
return NULL;
if (__envcnt >= __envsiz)
{
__envsiz = (__envsiz + 64) & ~63;
if (!(environ = realloc (environ, __envsiz * sizeof (char *))))
{
/* We're in a bit of a mess now, having lost the environ space.
Just reinitialise it and return failure. */
if (environ = malloc (sizeof (char *)))
*environ = 0;
return NULL;
}
}
environ[__envcnt] = NULL;
add:
if (!v)
memcpy (t, s, k);
else
{
memcpy (t, s, i);
t[i - 1] = '=';
memcpy (t + i, v, j);
}
return (t + i);
}
char *
__chkenv (const char *s)
{
register char **e, *t;
if (!environ)
{
/* This shouldn't be possible, unless we've been called
prior to unixinit setting environ. */
if (!(environ = malloc (sizeof (char *))))
return NULL;
*environ = NULL;
__envcnt = 0;
__envsiz = 1;
}
for (e = environ; t = *e; e++)
if (!__cmpenv (t, s))
{
while (*t++ != '=');
return (t);
}
return (0);
}
int
__intenv (const char *s, register int c)
{
int r[10];
char buf[256];
char *b;
_kernel_oserror *e;
if (c && (b = __chkenv (s)))
goto found;
b = buf;
r[0] = (int) s;
r[1] = (int) b;
/* One less than buf size so can zero terminate below. */
r[2] = sizeof (buf) - 1;
r[3] = 0;
r[4] = 3;
if (e = os_swi (OS_ReadVarVal, r))
{
__seterr (e);
return (0);
}
b[r[2]] = '\0';
b = __addenv (s, b);
found:
#ifdef DEBUG
os_print ("read var : ");
os_print ((char *) s);
os_print (" = ");
os_print (b);
os_print ("\r\n");
#endif
r[0] = 10;
r[1] = (int) b;
if (e = os_swi (OS_ReadUnsigned, r))
{
__seterr (e);
return (0);
}
return (r[2]);
}
char *
__getenv (const char *s, register int c)
{
int r[10];
char buf[256];
char *b;
_kernel_oserror *e;
if (c && (b = __chkenv (s)))
return (b);
else
b = buf;
r[0] = (int) s;
r[1] = (int) b;
/* One less than buf size so can zero terminate below. */
r[2] = sizeof (buf) - 1;
r[3] = 0;
r[4] = 3;
if (e = os_swi (OS_ReadVarVal, r))
{
__seterr (e);
return (0);
}
b[r[2]] = 0;
return (__addenv (s, b));
}
char *
getenv (const char *s)
{
return (__getenv (s, -1));
}